package com.zxt.spring_ai_demo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.metadata.ChatResponseMetadata;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
class MyController {
    // 会话客户端
    @Autowired
    private ChatClient chatClient;

//    public MyController(ChatClient.Builder chatClientBuilder) {
//        this.chatClient = chatClientBuilder
//                .defaultSystem("k哥是Java培训机构讲师，长得帅气逼人，声音浑厚有力，技术超群，授课幽默风趣")
//                .build();
//    }

    /**
     * 一次性响应
     *
     * @param userInput
     * @return
     */
    @GetMapping("/ai")
    String generation(@RequestParam("text") String userInput) {
        return this.chatClient.prompt(
                        new Prompt(
                                new UserMessage("刘德华是谁"),
                                new AssistantMessage("刘德华是一位多才多艺的艺人，不仅在音乐上取得了巨大成功，还在其他领域展现了非凡魅力。"),
                                new UserMessage(userInput)
                        )
                )
//                .system("k哥是国内排名前三的Java培训机构讲师，长得帅气逼人，声音浑厚有力，技术超群，授课幽默风趣")
//                .user(userInput)
                .call()
                .content();
    }

    /**
     * 流式响应
     *
     * @param userInput produces：告诉前端我响应回来的内容就是普通文本，编码格式是UTF-8
     * @return
     */
    @GetMapping(value = "/stream", produces = "text/plain;charset=UTF-8")
    Flux<String> stream(@RequestParam("text") String userInput) {
        return this.chatClient.prompt()
                .user(userInput)
                .stream()
                .content();
    }

    /**
     * 使用一次性输出获取更多信息
     *
     * @param userInput
     * @return
     */
    @GetMapping("/getChatResponse")
    String getChatResponse(@RequestParam("text") String userInput) {
        ChatClient.CallResponseSpec call = this.chatClient.prompt()
                .user(userInput)
                .call();
        // 聊天的响应对象，包含更多响应信息
        ChatResponse chatResponse = call.chatResponse();
        ChatResponseMetadata metadata = chatResponse.getMetadata(); // 获取响应回来的元数据信息
        System.out.println(metadata);
        String result = chatResponse.getResult().getOutput().getText();
        return result;
    }

    /**
     * 使用流式输出获取更多信息
     *
     * @param userInput
     * @return
     */
    @GetMapping(value = "/streamGetChatResponse", produces = "text/plain;charset=UTF-8")
    Flux<String> streamGetChatResponse(@RequestParam("text") String userInput) {
        Flux<ChatResponse> streamResponseSpec = this.chatClient.prompt()
                .user(userInput)
                .stream().chatResponse();
        // 把Flux<ChatResponse> ==> Flux<String>
        Flux<String> flux = streamResponseSpec.map(ChatResponse -> {
            System.out.println(ChatResponse.getMetadata());
            return ChatResponse.getResult().getOutput().getText();
        });
        return flux;
    }
}